package com.bana.fvs.app.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.base.BaseFragment;
import com.bana.fvs.app.enter.SetBaseUrlActivity;
import com.bana.fvs.app.enter.SetBaseUrlActivity_ViewBinding;
import com.bana.fvs.app.main.entity.UserInfoBean;
import com.bana.fvs.app.module.info.entity.LogOutBean;
import com.bana.fvs.app.module.info.page.AboutActivity;
import com.bana.fvs.app.module.info.page.ChangePwdActivity;
import com.bana.fvs.app.module.info.page.UserInfoActivity;
import com.bana.fvs.app.mvp.contract.UserInfoContract;
import com.bana.fvs.app.mvp.presenter.UserInfoPresenter;
import com.bana.fvs.app.util.UserInfoUtils;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.GlobalContext;
import com.wei.commonlibrary.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Author : yanftch
 * Date : 2018/2/7
 * Time : 10:27
 * Desc : 用户信息Fragment
 */

public class MineFragment extends BaseFragment<UserInfoPresenter> implements UserInfoContract.IUserInfoView {
    private static final String TAG = "dah_MineFragment";
    @BindView(R.id.rl_userInfo)
    RelativeLayout mUserInfoLayout;
    @BindView(R.id.rl_changePwd)
    RelativeLayout mChangePwdLayout;
    @BindView(R.id.rl_version)
    RelativeLayout mVersionLayout;
    @BindView(R.id.userinfo_iv_icon)
    ImageView mHeaderIcon;
    @BindView(R.id.userinfo_tv_user)
    TextView mUserName;
    @BindView(R.id.userinfo_tv_career)
    TextView mCareer;
    @BindView(R.id.rl_seturl)
    RelativeLayout rlSeturl;
    Unbinder unbinder;


    @Override
    protected void initPrsenter() {
        mFPresenter.setView(this);
    }

    @Override
    protected int setLayout() {
        return R.layout.layout_fragment_mine;
    }

    @Override
    protected void initView(View rootView) {
        mUserInfoLayout.setOnClickListener(this);
        mChangePwdLayout.setOnClickListener(this);
        mVersionLayout.setOnClickListener(this);
        rlSeturl.setOnClickListener(this);
    }

    @Override
    protected void initWidget() {

    }

    @Override
    public void onResume() {
        super.onResume();
        setUserInfo();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            setUserInfo();
        }
    }

    private void setUserInfo() {
        UserInfoBean userInfo = UserInfoUtils.getInstance().getUserInfo(mContext);
        if (userInfo != null) {
            mUserName.setText(userInfo.getYhmc());
            mCareer.setText(userInfo.getJgmc());
        }
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.rl_userInfo:
                ((BaseActivity) getActivity()).startActivity(UserInfoActivity.class);
                break;
            case R.id.rl_changePwd:
                ((BaseActivity) getActivity()).startActivity(ChangePwdActivity.class);
                break;
            case R.id.rl_version:
//                EquipmentDetailActivity.startAction(mContext,1,1);
                ((BaseActivity) getActivity()).startActivity(AboutActivity.class);
                break;
            case R.id.rl_seturl:
                startActivity(new Intent(getActivity(), SetBaseUrlActivity.class).putExtra("type",1));
                break;
        }

    }

    @Override
    public void onError(CommException e) {
        ToastUtil.showShort(GlobalContext.context, e.getMessage());
    }

    @Override
    public void onResponse(LogOutBean outBean) {

    }

    @Override
    public void onUpdatePhoneSuccess(String newPhone) {

    }

    @Override
    public void onUserInfoSuccess(UserInfoBean userInfoBean) {
        if (null != userInfoBean) {
            fillValues(userInfoBean);
        }
    }

    private void fillValues(UserInfoBean bean) {
        mUserName.setText(bean.getYhmc());
        mCareer.setText(bean.getJgmc());
    }


}
